package exam.web.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

import exam.common.util.CommonUtil;
import exam.common.util.NullCheckUtil;
import exam.model.dao.QuestionDAO;
import exam.model.entity.TQuestionEntity;
import exam.model.entity.TQuestionExamEntity;
import exam.web.action.base.QuestionExaminationActionBase;

public class CreateQuestionExaminationAction extends QuestionExaminationActionBase {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private List<TQuestionEntity> listQuestionEntity;
	
	private QuestionDAO questionDAO = new QuestionDAO();
	
	private String examId;
	
	private String subQuestId;
	
	private Map<String, Object> session;
	
	private List<String> listQuestionId;
	
	@SuppressWarnings("unchecked")
	public String execute() throws Exception {
		// get session
		session = ActionContext.getContext().getSession();
		// thiet lap session examId
		String examIdLocal = request.getParameter("examId");
		examId = (String) session.get("examId");
		if (NullCheckUtil.isNotNull(examIdLocal)
				&& CommonUtil.lengthObject(examIdLocal.trim()) > 0) {
			if(NullCheckUtil.isNUll(examId) 
					|| !examId.equals(examIdLocal)) {
				session.put("examId", examIdLocal);
				listQuestionId = new ArrayList<String>();
				session.put("listQuestionId", listQuestionId);
			}
		}
		// thiet lap session listQuestionId
		listQuestionId = (List<String>) session.get("listQuestionId");
		// button save
		String save = request.getParameter("button_save");
		if (NullCheckUtil.isNUll(save)) {
			// arrayQuestId
			String[] arrayQuestId = request.getParameterValues("arrayQuestId");
			if (NullCheckUtil.isNotNull(arrayQuestId)) {
				for(int i = 0; i < arrayQuestId.length; i++) {
					listQuestionId.add(arrayQuestId[i]);
				}
				session.put("listQuestionId", listQuestionId);
			}
			// not_in_query
			String not_in_query = "";
			StringBuffer sbListQuestId = null;
			if (listQuestionId.size() > 0) {
				sbListQuestId = new StringBuffer(listQuestionId.toString());
				sbListQuestId.replace(0, 1, "(").replace(sbListQuestId.length()-1, sbListQuestId.length(), ")");
				not_in_query = " not in" + sbListQuestId;
			}
			// thiet lap session subQuestId
			String subQuestIdLocal = request.getParameter("subQuestId");
			if (NullCheckUtil.isNotNull(subQuestIdLocal)) {
				subQuestId = (String) session.get("subQuestId");
				if (NullCheckUtil.isNUll(subQuestId)
						|| !subQuestId.equals(subQuestIdLocal)) {
					session.put("subQuestId", subQuestIdLocal);
				}
			}
			subQuestId = (String) session.get("subQuestId");
			// cau truy van
			String query = "select q from TQuestionEntity q left outer join q.subQuestionEntity " +
					"where q.subQuestId="+subQuestId;
			if (CommonUtil.lengthObject(not_in_query) > 0) {
				query += " and q.questionId " + not_in_query;
			}
			listQuestionEntity = questionDAO.selectListQuestion(query);
		} else {
			for(int i = 0; i < listQuestionId.size(); i++) {
				tQuestionExamEntity = new TQuestionExamEntity();
				tQuestionExamEntity.setExamId(Long.valueOf(examId));
				tQuestionExamEntity.setQuestionId(Long.valueOf(listQuestionId.get(i)));
				questionExamDAO.insertQuestionExam(tQuestionExamEntity);
			}
			// xoa session
			session.remove("listQuestionId");
			session.remove("examId");
			session.remove("subQuestId");
			System.out.println(listQuestionId.toString());
		}
		return SUCCESS;
	}

	public List<TQuestionEntity> getListQuestionEntity() {
		return listQuestionEntity;
	}

	public void setListQuestionEntity(List<TQuestionEntity> listQuestionEntity) {
		this.listQuestionEntity = listQuestionEntity;
	}
}
